package com.portalEsporteClubeBahia.action;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import com.portalEsporteClubeBahia.app.ApplicationException;
import com.portalEsporteClubeBahia.app.bo.NewsBO;
import com.portalEsporteClubeBahia.app.vo.NewsVO;

public class FindAllNewsAC extends HttpServlet {

	private static final long serialVersionUID = -3202449675379643208L;
	
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}
	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		JSONObject returning = new JSONObject();
		String status = "";
 		String message = "";
		JSONArray arrayData = new JSONArray();
		
		try {
			
			List<NewsVO> list = new NewsBO().findAll();
			
			if(list!=null){
				for(NewsVO news : list){
					JSONObject jsonObject = new JSONObject();
					jsonObject.put("id", news.getId());
					jsonObject.put("typeID", news.getTypeNews().getId());
					jsonObject.put("typeName", news.getTypeNews().getName());
					jsonObject.put("title", news.getTitle());
					jsonObject.put("date", news.getDateFormat());
					jsonObject.put("urlImage", news.getUrlImage());
					jsonObject.put("content", news.getContent());
					jsonObject.put("shortContenc", news.getShortContent());
					jsonObject.put("urlWebSite", news.getUrlWebSite());
					jsonObject.put("ranking", news.getRanking());
					
					arrayData.add(jsonObject);
				}
			}
			
			status =  "OK";
 			message = "";
			
		}catch (ApplicationException e) {
			status  =  "ERROR";
  			message = e.getMessage();
		}
		
		returning.put("status",status);
 		returning.put("message",message);
 		returning.put("data",arrayData);
		
		response.setContentType("application/json");
		response.setCharacterEncoding("utf-8");
		response.getWriter().write(returning.toString());
		
	}

}
